﻿    using System;
    using UnityEngine;

    [Serializable]
    public class CameraDrag
    {
        [SerializeField] protected Camera dragCamera;
        [SerializeField] private float distance;

        private Vector3 cameraOriginPos;
        private Vector3 startTouchPos;
        private Vector3 curTouchPos;

        protected Vector3 curDragComeraPos;
        protected Vector3 cameraTargetPos;
        protected bool isLaunchMove = false;

        private bool isMove;
        private Vector3 moveDetla;
        private int layerMask;

        private Ray cameraRay;
        //public Transform pointNode;

        public void Init()
        {
            getDistance();
        }


        private void getDistance()
        {
            cameraRay = dragCamera.ViewportPointToRay(new Vector2(0.5f, 0.5f));
            layerMask = 1 << LayerMask.NameToLayer("Ignore Raycast");
            RaycastHit hit;
            RaycastHit2D raycastHit2D = Physics2D.Raycast(dragCamera.transform.position, cameraRay.direction,
                Mathf.Infinity,
                layerMask);
            if (raycastHit2D)
            {
                distance = (dragCamera.transform.position - raycastHit2D.transform.position).magnitude;
                Debug.LogError("获取距离" + distance);
            }
            else if (Physics.Raycast(cameraRay, out hit, Mathf.Infinity,
                layerMask))
            {
                distance = (dragCamera.transform.position - hit.transform.position).magnitude;
                Debug.LogError("获取距离" + distance);
            }
            else
            {
                Debug.LogError("获取距离失败");
                distance = 10;
            }
        }


        public void OnGrag()
        {
            moveDetla = Vector3.zero;
            isCameraMove(out moveDetla);
        }

        bool isCameraMove(out Vector3 moveDetla)
        {
            moveDetla = Vector3.zero;
            if (Input.GetMouseButtonDown(0))
            {
                setBeginState();
            }
            else if (Input.GetMouseButton(0))
            {
                Debug.DrawLine(cameraRay.origin, cameraRay.origin + cameraRay.direction * 50, Color.red);
                Vector3 curMousePos = getMouseWorldPos();
                Vector3 detla = curMousePos - curTouchPos;
                if (detla.sqrMagnitude > 0)
                {
                    Vector3 temp = dragCamera.ScreenToWorldPoint(curMousePos);
                    onToutch(temp);
                    moveDetla = dragCamera.ScreenToWorldPoint(startTouchPos) - temp;

                    LaunchCameraMove();
                    // moveDetla = temp - dragCamera.ScreenToWorldPoint(startTouchPos);
                    curTouchPos = curMousePos;
                    return true;
                }
            }

            return false;
        }

        private void setBeginState()
        {
            Debug.DrawLine(cameraRay.origin, cameraRay.origin + cameraRay.direction * 50, Color.red);
            startTouchPos = getMouseWorldPos();
            curTouchPos = startTouchPos;
            cameraOriginPos = dragCamera.transform.position;
        }


        protected virtual void LaunchCameraMove()
        {
            cameraTargetPos = cameraOriginPos + moveDetla;
            curDragComeraPos = dragCamera.transform.position;
            isLaunchMove = true;
        }


        public void SetCameraPos(Vector3 result)
        {
            dragCamera.transform.position = result;
        }

        public void UpdataDrag()
        {
            OnCameraMove();
        }

        protected virtual void OnCameraMove()
        {
        }

        private Vector3 getMouseWorldPos()
        {
            Vector3 temp = Input.mousePosition;
            temp.z = distance;
            return temp;
        }

        private void onToutch(Vector3 pos)
        {
            // pointNode.transform.position = pos;
        }
    }
